// 350. 两个数组的交集 II

function intersect(nums1: number[], nums2: number[]): number[] {
  const map = new Map<number, number>();
  const result: number[] = [];

  for (const num of nums1) {
    if (map.has(num)) {
      let acount = map.get(num);
      map.set(num, ++acount);
    } else {
      map.set(num, 1);
    }
  }

  // console.log(map)
  for (const num of nums2) {
    if (map.has(num) && map.get(num)) {
      // 有并且计数不为0
      result.push(num);
      let acount = map.get(num);
      // console.log(acount)
      map.set(num, --acount);
    }
  }

  return result;
}

const nums31 = [1, 2, 2, 1],
  nums32 = [2, 2];
console.log(intersect(nums31, nums32));
